class OneClickService
       
  def initialize(map)
    @map = map
   
    @profile = { 
      :payment_profiles => { 
        :bill_to => customer_billing_address,
        :payment => customer_payment_options
      },
      :ship_to_list => customer_shipping_options
    }
  end
  
  def create
    external_cim_id = -1
        
    begin
      response = zgateway.create_customer_profile({:profile => @profile})
      
      if response.success?
        external_cim_id = response.authorization
      else
        GatewayResponse.create!(:message => response.message, 
                                :email   => @map[:email],  
                                :code    => response.params["code"] )    
      end
    rescue => e
      logger.info("OneClickService failed due to : #{e.inspect}" )
      GatewayResponse.create!(:message => e.inspect)
    end
    
    external_cim_id
  end
  
  def destroy
    # TODO: Load customer_profile_id from the db for the logged in user
    response = zgateway.delete_customer_profile(:customer_profile_id => find_customer_cim_id)
     # c = Customer.find current_user.id 
    # c.external_cim_id
    if response.success?
      # Customer.update_attributes(:external_cim_id => nil)
      @success = true
    else
      # throw exception here and ask user to try again later...
      @success = false
    end
  end
  
  # TODO: Update credit card, billing address, shipping address
  # Very involved: Refer the source code for AM
  # def update
  #   # c = Customer.find current_user.id 
  #   # c.external_cim_id
  #   response = zgateway.update_customer_profile(:profile => {:customer_profile_id => 12345, :email => 'new email address'})
  # 
  #    if response.success?
  #       # Customer.update_attributes(:external_cim_id => nil)
  #       @success = true
  #     else
  #       # throw exception here and ask user to try again later...
  #       @success = false
  #     end 
  # end
  
  private
  
  def customer_billing_address
    { 
       :name     => "#{@map[:first_name]} #{@map[:last_name]}",
       :address1 =>  @map[:billing_address],
       :city     =>  @map[:billing_city],
       :state    =>  @map[:billing_state],
       :zip      =>  @map[:billing_zip],
       :country  =>  'US',
     }
  end
  
  def customer_payment_options
    {
      :credit_card => ActiveMerchant::Billing::CreditCard.new(
                         :first_name => @map[:first_name],
                         :last_name  => @map[:last_name],
                         :number     => @map[:credit_card_number],
                         :month      => @map[:expiration_month],
                         :year       => @map[:expiration_year])
    }
  end
  
  def customer_shipping_options
    {
      :first_name => @map[:first_name], 
      :last_name  => @map[:last_name], 
      :address1   => @map[:shipping_address],
      :city       => @map[:shipping_city],
      :state      => @map[:shipping_state],
      :zip        => @map[:shipping_zip],
      :country    => 'US',
    }
  end
end